/**
 * Created by Administrator on 2017/9/7.
 */

$(document).ready(function () {

    var productId=locationSearcher('productId');
    var marketUserProductId=locationSearcher('marketUserProductId');

    var redTpl=$('#red-product-template').html();
    var redCmp=Handlebars.compile(redTpl);

    $.ajax({
        url:C.interface.redIndex,
        type:'get',
        dataType:'json',
        data:{
            token:C.token,
            productId:productId
        },
        success:function (response) {
            if (response.result === 'success'){
                var data=response.data;
                console.log(data);
                $('#red-product-info').html(redCmp(data));
                $('.now-pull').one('click',getRedPackage);
                $('.red-select').click(function () {
                    event.preventDefault();
                    location.href = 'share_detail.html?productId='+productId+'&marketUserProductId='+marketUserProductId;
                })
            }else if (response.result === 'login'){
                alert('登录过期请重新登录');
                againLogin();
            }else {
                alert(response.erroeMsg);
            }
        }
    });

    function getRedPackage() {
        $.ajax({
            url:C.interface.redGet,
            type:'get',
            dataType:'json',
            data:{
                productId:productId,
                token:C.token
            },
            success:function (response) {
                if (response.result === 'success'){
                    $('.now-pull').html('已领取');
                    $('.now-pull').addClass('none-pull').removeClass('now-pull').off('click');

                }else {
                    alert('您已经领取过了');
                }
            }
        })
    }




});

